/***************************************************************************
 *   Copyright (C) 2008 by JBox systems
 *   gabi@shaargiora.co.il
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

// #include <QDebug>

#include <boost/filesystem.hpp>

#include <iostream>

#include <QFileInfo>

#include <jb/std/memory.h>
#include <jb/std/log.h>

#include "downloader.h"
#include "config.h"

Downloader::Downloader(QObject *parent):
	QObject(parent),
	file(NULL),
	url(NULL)	 {
}

bool Downloader::openDownloadFile(QString filename) {

	// Check destination path exists
	if (!validatePath())
		return false;

	// Get filename from url if not given as parameter
	QFileInfo fileInfo(url->path());
	if (filename.size() == 0)
		filename = fileInfo.fileName();

	// Create full filename
	QString full_filename = (boost::filesystem::path(destination) /  filename.toStdString()).string().c_str();

	LOG("Downloading to " << full_filename.toStdString())
	// Remove existing file
	if (QFile::exists(full_filename)) {
		if (!CONFIG.overwrite()) {//overwrite check
			LOG("File exists. not overwriting")
			return false;
		}
		QFile::remove(full_filename);
	}

	file = new QFile(full_filename, this);

	// Open local file for writing.
	if (!file->open(QIODevice::Truncate|QIODevice::WriteOnly)) {
		DELETELATER(file)
		return false;
	}

	emit currentDownloadFilename(filename);
	return true;
}

bool Downloader::validatePath() {
	if (!boost::filesystem::exists(boost::filesystem::path(destination)))
		return boost::filesystem::create_directory(boost::filesystem::path(destination));
	return true;
}

void Downloader::removeFile() {
	if (file and file->exists()) {
		LOG("Removing file")
		file->remove();
	}
}

void Downloader::cancelDownload() {
	LOG("Canceling download")
}

void Downloader::downloadFile(std::string source, std::string destination) {
	this->destination = destination;
	url = new QUrl(source.c_str());
	LOG("Downloading " << url->toString().toStdString())
}
